var Diciprefab = [];
var DiciNum = 0;

cc.Class({
    extends: cc.Component,

    properties: {
        player:{
            default:null,
            type:cc.Node
        },
        dici:{
            default:null,
            type:cc.Prefab
        },

        timeLabel:{
            default:null,
            type:cc.Label,
        },
        fenshuLable:{
            default:null,
            type:cc.Label,
        },
        BgMusic:{
            default:null,
            url:cc.AudioClip,
        },
        JumpMusic:{
            default:null,
            url:cc.AudioClip,
        },
        // DieAudio:{
        //     default:bull,
        //     type:cc.AudioClip
        // },

        playJumpHeigh:30,
        qianghou:80,
        dicinum:0,
        dicijiange:140,
        GameTime:60,
        FenShu:0,
    },


    onLoad: function () {
        this.windinit();
        this.getzuoyoux();
    },

    // 初始化窗口
    windinit: function (){
        // var manager = cc.director.getCollisionManager();
        // manager.enabled = true;
        // manager.enabledDebugDraw = true;

        // 初始化精灵的位置
        this.player.setPosition(-this.node.width/2 + this.qianghou,this.node.height/2- this.dicijiange);
        this.dicinum = 0;


        for(var i=0;i<8;i++){
            this.newDici();
        }


        //销毁多余的地刺节点
        // 监听全局自定义事件
        cc.systemEvent.on("XiaohuiDici", function (event) {
            console.log('要销毁了');
            while (Diciprefab.length){
                Diciprefab.pop();
            }
            DiciNum=0;
        });

        this.Danjishi();

    },

    // 监听用户左右点击事件
    getzuoyoux:function(){
        // 使用事件名来注册
        this.node.on('touchstart',function(event){
            this.DiciQianjing();

            var jumpmusic = cc.audioEngine.play(this.JumpMusic,false,0.6);

            //定义全局自定义事件
            // cc.systemEvent.emit("qianjin", '');

            var x = event.getLocationX(); 
            var maxX = this.node.width/2;
            if(x > maxX){
                // console.log("右边");
                this.playerMovyou();
            }else{
                // console.log("左边");
                this.playerMovzuo();
            }

            this.newDici();

            this.FenShu++;
            this.fenshuLable.string = this.FenShu;
            cc.sys.localStorage.setItem('fenshu',this.FenShu);
        },this);
    },




    // 精灵左调
    playerMovzuo:function(){
        var gerlift = cc.moveTo(0.2,cc.v2(-this.node.width/2 + this.qianghou,this.player.getPosition().y));
        var gotozuo1 = cc.moveTo(0.1,cc.v2(-this.node.width/2 + this.qianghou+30,this.player.getPosition().y));
        var gotozuo2 = cc.moveTo(0.2,cc.v2(-this.node.width/2 + this.qianghou,this.player.getPosition().y));
        var squezuo = cc.sequence(gotozuo1,gotozuo2);
        if(this.player.rotationY == 0){
            this.player.runAction(squezuo);
        }else{
            this.player.runAction(gerlift);
        }
        this.player.rotationY=0;
    },
    // 精灵右跳
    playerMovyou:function(){
        var gerright = cc.moveTo(0.2,cc.v2(this.node.width/2 - this.qianghou,this.player.getPosition().y));
        var gotoyou1 = cc.moveTo(0.1,cc.v2(this.node.width/2 - this.qianghou - 30,this.player.getPosition().y));
        var gotoyou2 = cc.moveTo(0.1,cc.v2(this.node.width/2 - this.qianghou,this.player.getPosition().y));
        var squeyou = cc.sequence(gotoyou1,gotoyou2);
        if(this.player.rotationY == 180){
            this.player.runAction(squeyou);
        }else{
            this.player.runAction(gerright);
        }
        this.player.rotationY=180;
    },

    // 随机生成地刺
    newDici: function(){
        this.dicinum += 1;
        var newDici = cc.instantiate(this.dici);
        Diciprefab[DiciNum] = newDici;
        DiciNum++;
        // newDici.parent = cc.director.getScene();

        this.node.addChild(newDici);

        var ranD = Math.random();

        if(ranD >= 0.5){
            newDici.rotationY = 0;
        }else{
            newDici.rotationY = 180;
        }
        newDici.setPosition(this.diciRandPosi(ranD));
    },

    // 生成随机的位置坐标
    diciRandPosi: function(ranD){
            var ranX = 0;
            var ranY = 0;
        if(ranD >= 0.5){
            ranX = this.node.width/2 - this.qianghou;
        }else{
            ranX = -this.node.width/2 + this.qianghou;
        }

        if(this.dicinum < 9){
            ranY = (this.node.height/2) - (this.dicinum * this.dicijiange) - this.dicijiange;
        }else{
            ranY = (this.node.height/2) - (8 * this.dicijiange) - this.dicijiange;
        }

        return cc.v2(ranX,ranY);
    },

    
    //倒计时
    Danjishi:function () {
        this.schedule(function() {
            this.GameTime--;
            this.timeLabel.string = '倒计时：' + this.GameTime;
            if (this.GameTime <= 0){
                cc.director.loadScene('OverScene');
            }

        }, 1);
    },

    //所有地刺向前移动
    DiciQianjing:function () {
        for (var x in Diciprefab){
            var goAction = cc.moveBy(0.2,cc.v2(0,140));
            Diciprefab[x].runAction(goAction);
        }
    },


    //    用户自定义函数
    //监听是否撞击了
    // onCollisionEnter: function (other, self) {
    //     console.log('碰到了');
    //     cc.audioEngine.play(this.DieAudio,false,0.8)
    //     cc.systemEvent.emit("XiaohuiDici", '');
    //     cc.director.loadScene("OverScene");
    // },




    start:function () {
        this.FenShu = 0;
        this.current = cc.audioEngine.play(this.BgMusic,true,0.6);
        // current.audioEngine.volume(0.5);

        // console.log(this.current.audioEngine.isPlaying());
    },

    onDestroy:function () {
        cc.audioEngine.stop(this.current);
    },

    update: function (dt) {

    },
});
